package liwen.zhao.controller;

import liwen.zhao.common.jd.utils.CookieUtils;
import liwen.zhao.common.jd.utils.UUIDUtil;
import liwen.zhao.common.jd.utils.VerifyCode;
import liwen.zhao.common.jd.vo.SysResult;
import liwen.zhao.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
@RestController
@RequestMapping("/user")
public class CommonUserController {
    @Autowired
    UserService userService;
    @Autowired
    VerifyCode verifyCode;
    @RequestMapping(value = "/getUser", method = RequestMethod.GET)//value = "getUser", method = RequestMethod.GET
    public SysResult getUser(String cookieV) {
        SysResult sysResult = userService.getUser(cookieV);
        return sysResult;
    }
    
    @RequestMapping(value = "/getValiCodeImg", method = RequestMethod.GET)
    public void getValicodeImg(HttpServletRequest request, HttpServletResponse response) {
        String valiCodeId = CookieUtils.getCookieValue(request, "valiCodeId");
        if (valiCodeId == null || valiCodeId.equals("")) {
            valiCodeId = "valiCodeId_"+ UUIDUtil.getUUID();
            CookieUtils.setCookie(request, response, "valiCodeId", valiCodeId);
        }
        try {
            verifyCode.drawImage(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        userService.saveValiCode(valiCodeId, verifyCode);
    }
    
}
